Type.registerNamespace('ModWatch');

ModWatch.OnButtonClickModAlertBehavior = function(element) { 
    ModWatch.OnButtonClickModAlertBehavior.initializeBase(this, [element]);

	// Click handler for the target control.
	this._clickHandler = null;

	this._oldScript = null;
}

ModWatch.OnButtonClickModAlertBehavior.prototype = {

	_onClick : function(e)
	{
		if (this.get_element() && !this.get_element().disabled)
		{
			// Note that this behavior was triggered (for later)
			ModWatch.OnButtonClickModAlertBehavior._clickedBehavior = this;
		}
	},

	initialize : function()
	{
		ModWatch.OnButtonClickModAlertBehavior.callBaseMethod(this, 'initialize');
		var element = this.get_element();

		// Attach the handler
		this._clickHandler = Function.createDelegate(this, this._onClick);
		$addHandler(element, "click", this._clickHandler);

		// remove any existing script
		this._oldScript = element.getAttribute("onclick");
		if (this._oldScript)
			element.setAttribute("onclick", null);

		// Hook into the ASP.NET WebForm_OnSubmit function to display the confirm dialog prior to submission
		if ((typeof(WebForm_OnSubmit) == 'function') && !ModWatch.OnButtonClickModAlertBehavior._originalWebForm_OnSubmit)
		{
			// Hook WebForm_OnSubmit normally
			ModWatch.OnButtonClickModAlertBehavior._originalWebForm_OnSubmit = WebForm_OnSubmit;
			WebForm_OnSubmit = ModWatch.OnButtonClickModAlertBehavior.WebForm_OnSubmit;
		}
	},

	dispose : function()
	{
		if (this._clickHandler)
		{
			$removeHandler(this.get_element(), "click", this._clickHandler);
			this._clickHandler = null;
		}

		if (this._oldScript)
		{
			this.get_element().setAttribute("onclick", this._oldScript);
			this._oldScript = null;
		}

		ModWatch.OnButtonClickModAlertBehavior.callBaseMethod(this, 'dispose');
	}

}

ModWatch.OnButtonClickModAlertBehavior.registerClass('ModWatch.OnButtonClickModAlertBehavior', ModWatch.ModAlertBehaviorBase);


ModWatch.OnButtonClickModAlertBehavior.WebForm_OnSubmit = function()
{
	// Wraps ASP.NET's WebForm_OnSubmit in order to check for form changes prior to submission
	// Returns the result of original WebForm_OnSubmit
	var result = ModWatch.OnButtonClickModAlertBehavior._originalWebForm_OnSubmit();
	if (result && ModWatch.OnButtonClickModAlertBehavior._clickedBehavior)
		result = ModWatch.OnButtonClickModAlertBehavior._clickedBehavior.verifyChanges();
	ModWatch.OnButtonClickModAlertBehavior._clickedBehavior = null;
	return result;
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
